package com.wt.struggle.goodbye.main.activity

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.os.Message
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.xin.lv.yang.utils.utils.DataUtil
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.bao_ming_layout.view.*
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.part_activity_layout.*
import java.security.KeyStore


/**
 * 活动详情页面
 */
class PartActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.PART_DETAILS -> {

                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val newsInfo: NewsInfo = gson!!.fromJson(result, NewsInfo::class.java)
                        showUserInfo(newsInfo)
                    }
                }
            }

            Send.PART_DETAILS_BAOMING -> {
                if (netInfo.code == Config.SUCCESS) {
                    buttonBaoMing.text = "已报名"
                    buttonBaoMing.isClickable = false
                    showToastShort("报名成功！！")
                }

            }

        }
    }

    private fun showUserInfo(newsInfo: NewsInfo) {
        tvPartTitle.text = newsInfo.title
        tvPartContent.text = newsInfo.des
        tvPartRule.text = newsInfo.rules
        val endStr = newsInfo.endtimeStr
        val endLong = DataUtil.stringTimeToLong(endStr, "yyyy-MM-dd HH:mm:ss")
        val indexTime = System.currentTimeMillis()

        if (indexTime > endLong) {
            buttonBaoMing.text = "已结束"
            buttonBaoMing.isClickable = false
            buttonBaoMing.setBackgroundResource(R.color.gray)
            buttonBaoMing.setTextColor(resources.getColor(R.color.black))
        } else {
            buttonBaoMing.text = "报名"
            buttonBaoMing.isClickable = true
            buttonBaoMing.setBackgroundResource(R.color.red)
            buttonBaoMing.setTextColor(resources.getColor(R.color.witch))
        }

        ImageUtil.getInstance().loadRoundCircleImage(this, imageGiftPic, Config.GET_IMAGE_URL + newsInfo.giftPic1, 0, 4)

        ImageUtil.getInstance().loadRoundCircleImage(this, imageGift2, Config.GET_IMAGE_URL + newsInfo.giftPic2, 0, 4)

        ImageUtil.getInstance()
            .loadImageNoTransformation(this, imagePartBg, R.drawable.part_bg, Config.GET_IMAGE_URL + newsInfo.coverPic)

        removeLoadDialog()

    }

    var info: NewsInfo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.part_activity_layout)

        info = intent.getParcelableExtra<NewsInfo>("info")

        tvTitle.text = info!!.title

        initClick()

        getDetailsById(info!!.toId)

    }

    /**
     * 获取活动详情
     */
    private fun getDetailsById(id: Int?) {
        send!!.getPartDetails(id, handler)
        showLoadDialog("获取中")

    }

    private fun initClick() {
        imageBack.setOnClickListener {
            finish()
        }

        buttonBaoMing.setOnClickListener {

            caoZuoBaoMing()
        }
    }

    private fun caoZuoBaoMing() {
        send!!.partBaoMing(info!!.id!!, handler)
    }


    private fun showSuccessDialog() {
        val dialogView = layoutInflater.inflate(R.layout.bao_ming_layout, null)
        val build: AlertDialog.Builder = AlertDialog.Builder(this)
        build.setView(dialogView)
        build.setCancelable(false)
        val dialog = build.create()
        dialog.show()

        dialogView.buttonKnown.setOnClickListener {
            val intent = Intent(this, BaoMingDetailsActivity::class.java)
            startActivity(intent)
            dialog.dismiss()
        }

        dialogView.imageClose.setOnClickListener {
            dialog.dismiss()
        }


    }
}